using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using RefreshModule;

namespace OnlineAnadolu
{
    [Refresh()]
    public partial class KategoriEkleme : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (Session["usertype"].ToString() != "1")
            {
                Response.Redirect("HataSayfasi.aspx");
            }
            if (!IsPostBack)
            {
                FillAuthors(Menu1.Items[0]);

            }
            Menu1.DataBind();

        }
        void FillAuthors(MenuItem node)
        {
            string connString = System.Configuration.ConfigurationSettings.AppSettings["MyCon"];
            SqlConnection connection = new SqlConnection(connString);
            SqlCommand command = new SqlCommand("Select * From categories where parentid is null and status=1", connection);
            SqlDataAdapter adapter = new SqlDataAdapter(command);
            DataSet authors = new DataSet();
            adapter.Fill(authors);
            if (authors.Tables.Count > 0)
            {
                foreach (DataRow row in authors.Tables[0].Rows)
                {
                    MenuItem newNode = new MenuItem();
                    newNode.Text = row["categoryname"].ToString();
                    newNode.Value = (row["categoryid"]).ToString();

                    // newNode.NavigateUrl = "../UrunGoster.aspx?CatID=" + (row["categoryid"]).ToString();

                    // newNode.SelectAction = TreeNodeSelectAction.Expand;
                    node.ChildItems.Add(newNode);

                    FillTitlesForAuthors(newNode);

                }
            }
        }

        void FillTitlesForAuthors(MenuItem node)
        {
            string authorID = node.Value.ToString();
            string connString = System.Configuration.ConfigurationSettings.AppSettings["MyCon"];
            SqlConnection connection = new SqlConnection(connString);
            SqlCommand command = new SqlCommand("Select categoryname, categoryid From categories Where parentid = '" + authorID + "' and status=1", connection);
            SqlDataAdapter adapter = new SqlDataAdapter(command);
            DataSet titlesForAuthors = new DataSet();
            adapter.Fill(titlesForAuthors);
            if (titlesForAuthors.Tables.Count > 0)
            {
                foreach (DataRow row in titlesForAuthors.Tables[0].Rows)
                {
                    MenuItem newNode = new MenuItem();
                    newNode.Text = row["categoryname"].ToString();
                    newNode.Value = (row["categoryid"]).ToString();
                    //    newNode.NavigateUrl = "../UrunGoster.aspx?CatID=" + (row["categoryid"]).ToString();

                    // newNode.PopulateOnDemand = false;
                    //   newNode.SelectAction = TreeNodeSelectAction.None;
                    node.ChildItems.Add(newNode);
                    //node.NavigateUrl = "../UrunGoster.aspx?CatID=" + (row["categoryid"]).ToString();

                    FillTitlesForAuthors(newNode);
                }
            }
        }

        protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
        {
            lb_EklemeKategori.Text = Menu1.SelectedItem.Text + " in altina ekliyorsunuz";
        }

        protected void bt_Kaydet_Click(object sender, EventArgs e)
        {
            DBNull dr = DBNull.Value;
            Database db = new Database();
            string sr = "";
            if (lb_EklemeKategori.Text != "")
            {
                if (lb_EklemeKategori.Text != "Bir Kategori Seciniz in altina ekliyorsunuz")
                {
                    if (Menu1.SelectedItem.Text != "")
                        sr = "INSERT INTO categories(parentid, categoryname,description) VALUES ('" + Menu1.SelectedValue + "',N'" + tb_CategoryName.Text + "',N'" + tb_Tanim.Text + "')";
                    db.insert(sr);
                    lb_Uyari.Text = "Veri Eklendi";
                    Menu1.DataBind();
                }
                else
                {


                    sr = "INSERT INTO categories(categoryname,description) VALUES ('" + tb_CategoryName.Text + "','" + tb_Tanim.Text + "')";
                    db.insert(sr);
                    lb_Uyari.Text = "Ekleme basarili";
                    Menu1.DataBind();
                }
            }
            Menu1.Items.Clear();
            MenuItem mr = new MenuItem();
            Menu1.Items.Add(mr);
            FillAuthors(Menu1.Items[0]);
            Menu1.DataBind();


        }

        protected void ch_Ekle_CheckedChanged(object sender, EventArgs e)
        {
            bt_Kaydet.Visible = true;
            bt_Sil.Visible = false;
            bt_Update.Visible = false;
          
           
        }

        protected void ch_Sil_CheckedChanged(object sender, EventArgs e)
        {
            bt_Kaydet.Visible = false;
            bt_Sil.Visible = true;
            bt_Update.Visible = false;
            string sr = "Select categoryname,description  from categories where categoryid='" + Menu1.SelectedValue + "'";
            Database db = new Database();
            DataTable dt = new DataTable();
            dt = db.select(sr);
            foreach (DataRow dr in dt.Rows)
            {
                tb_CategoryName.Text = dr["categoryname"].ToString();
                tb_Tanim.Text = dr["description"].ToString();
            }
        }

        protected void ch_Guncelle_CheckedChanged(object sender, EventArgs e)
        {
            bt_Kaydet.Visible = false;
            bt_Sil.Visible = false;
            bt_Update.Visible = true;
            string sr = "Select categoryname,description  from categories where categoryid='" + Menu1.SelectedValue + "'";
            Database db = new Database();
            DataTable dt = new DataTable();
            dt = db.select(sr);
            foreach (DataRow dr in dt.Rows)
            {
                tb_CategoryName.Text = dr["categoryname"].ToString();
                tb_Tanim.Text = dr["description"].ToString();
            }

        }

        protected void bt_Sil_Click(object sender, EventArgs e)
        {
            Database db = new Database();
            string sr = "UPDATE categories SET status=0 where categoryid='" + Menu1.SelectedValue + "'";
            db.insert(sr);
            lb_Uyari.Text = "Veri Silindi.";
            Menu1.Items.Clear();
            MenuItem mr = new MenuItem();
            Menu1.Items.Add(mr);
            FillAuthors(Menu1.Items[0]);
            Menu1.DataBind();

        }

        protected void bt_Update_Click(object sender, EventArgs e)
        {
            Database db = new Database();
            string sr = "UPDATE categories SET categoryname =N'"+tb_CategoryName.Text+"', description =N'"+tb_Tanim.Text+"' where categoryid='"+Menu1.SelectedValue+"'";
            db.insert(sr);
            lb_Uyari.Text = "Veri Guncellendi.";

            Menu1.Items.Clear();
            MenuItem mr = new MenuItem();
            Menu1.Items.Add(mr);
            FillAuthors(Menu1.Items[0]);
            Menu1.DataBind();

        }
    }
}
